package cn.sciento.farm.devicemanager.service.impl;

import cn.sciento.farm.devicemanager.dao.entity.DevScreenDeviceRel;
import cn.sciento.farm.devicemanager.dao.mapper.DevScreenDeviceRelMapper;
import cn.sciento.farm.devicemanager.service.ScreenDeviceRelService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class ScreenDeviceRelServiceImpl implements ScreenDeviceRelService {

    @Autowired
    private DevScreenDeviceRelMapper screenDeviceRelMapper;

    @Override
    public int deleteByScreenId(Long screenId) {
        QueryWrapper queryWrapper = new QueryWrapper<DevScreenDeviceRel>();
        queryWrapper.eq("screen_id",screenId);
        int count = screenDeviceRelMapper.delete(queryWrapper);
        return count;
    }

    @Override
    public int create(DevScreenDeviceRel rel) {
        return screenDeviceRelMapper.insert(rel);
    }

    @Override
    public List<String> getDeviceIds(Long screenId) {
        QueryWrapper queryWrapper = new QueryWrapper<DevScreenDeviceRel>();
        queryWrapper.eq("screen_id",screenId);
        List<DevScreenDeviceRel> rels = screenDeviceRelMapper.selectList(queryWrapper);
        List<String> deviceIds = rels.stream().map(rel->{
            return rel.getDeviceId();
        }).collect(Collectors.toList());

        return deviceIds;
    }
}
